import { request } from "../utils/request";
import { action, observable } from 'mobx';

const SIZE = 10

export default class HomeStore {
  page: number = 1
  @observable homeList: ArticleSimple[] = [];
  @observable refreshing: boolean = false;

  @action
  resetPage = () => {
    // this.page = 1;
  }

  requestHomeList = async () => {
    // refreshing 为一个标记 实现类似瀑布流的效果  false 才进入请求接口  每次请求成功时 变为true   请求的finally里变成false
    if (this.refreshing) {
      return;
    }
    try {
      this.refreshing = true;
      const params = {
        page: this.page,
        size: SIZE
      }
      const { data } = await request('homeList', params)
      if (data?.length) {
        if (this.page === 1) {
          this.homeList = data;
        } else {
          this.homeList = [...this.homeList, ...data];
        }
        this.page++
      } else {
        console.log('已经加载完了，没有更多数据');
      }
    } catch (error) {
      console.log(error);
    }finally {
      this.refreshing = false;
  }
  }
}

//  new HomeStore